Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Using forEach method to target each button independently and display it's corresponding "share-container"

I got this code to work but it I got a bug. It is opening and closing all 12 elements collectively (".share-btn-container"). Trying to get it to target only the button clicked, and it's corresponding share-btn-container. I suspect it has something to do with the "let sharePanelOpen = true".

//HTML 
//12 of those divs, all sharing the "share-btns" class and "share-btn-container" class.

<button id ="share-song1" class ="share-btns"><span class ="fa">&#xf1e0</span></button> 
            <div id ="share-container1" class ="share-btn-container">
            <a href="#" id="facebookshare1" class ="facebook-btn"><i class ="fab fa-facebook"></i></a>
            <a href="#" id="whatsappshare1" class ="whatsapp-btn"><i class ="fab fa-whatsapp"></i></a>
            <a href="#" id="twittershare1"  class ="twitter-btn"><i class ="fab fa-twitter"></i></a>
            <a href="#" id="linkedinshare1" class ="linkedin-btn"><i class ="fab fa-linkedin"></i></a>
         </div>

//Javascript

let sharePanelOpen = false;

const shareButtons = document.querySelectorAll('.share-btns');
const shareContainers = document.querySelectorAll('.share-btn-container');

function openSharePanel () {
    shareContainers.forEach((container) => {
      console.log(container);
      sharePanelOpen = true;
      container.classList.add('share-btn-container-active');          
    })
      shareButtons.forEach((button) => {
        button.classList.add('share-btns-active');
    })
}

function closeSharePanel () {
    shareContainers.forEach((container) => {
      sharePanelOpen = false;
      container.classList.remove('share-btn-container-active');          
    })
      shareButtons.forEach((button) => {
        button.classList.remove('share-btns-active');
    })
}

shareButtons.forEach((button, i) => {
  button.addEventListener('click', () => { 
      togglesharePanel();    
   })
})

function togglesharePanel() {sharePanelOpen ? closeSharePanel() : openSharePanel();}

//CSS

.share-btn-container {
    display: none;
}

.share-btn-container-active {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-content: center;
    position: absolute;
    top: 78px;
    left: 66px;
    background: rgb(30,10,40);
    border:  1px solid rgb(200,200,200);
    border-radius: 6%;
    width: 14.2rem;
    height: 7rem;
    padding: 0.5rem;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    z-index: 4;
    cursor: auto;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You don't even need to use sharePanelOpen just pass reference of sharedContainer of corresponding button using it's index when you register click event for each button. Then in togglesharePanel() function just use toggle method of classList to add / remove 'share-btn-container-active' class

for example

const shareButtons = document.querySelectorAll('.share-btns');
const shareContainers = document.querySelectorAll('.share-btn-container');


shareButtons.forEach((button, i) => {
  button.addEventListener('click', () => { 
      togglesharePanel(shareContainers[i]);    
   })
})

function togglesharePanel(shareContainersRef) {
  shareContainersRef.classList.toggle('share-btn-container-active')
}
about 4 years ago · Juan Pablo Isaza Relatório

0

const shareButtons = document.querySelectorAll('.share-btns');
const shareContainers = document.querySelectorAll('.share-btn-container');

shareButtons.forEach((button, i) => {
  button.addEventListener('click', () => { 
      togglesharePanel(shareContainers[i]);
      toggleshareBtns(shareButtons[i])    
   })
})

function togglesharePanel(shareContainersRef) {
  shareContainersRef.classList.toggle('share-btn-container-active')
  console.log(shareContainersRef);
}

function toggleshareBtns(buttonref) {
  buttonref.classList.toggle('share-btns-active')
} 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda